<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;
use Card\Controller\PublicController;

/**
 * 云名片照片
 */
class MyPhotosController extends PublicController {

    /**
     * 我上传的照片列表
     */
    public function PhotoWall() {
        $data = inputJson();
        // $data['phpsid'] = '9mtnc4p33ibsnhl0ep533jgoi5';

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //查用户id
        $user_id = $this->get_userId($data['phpsid']);
        if($user_id === false){
            errorOutputJson('j'.__LINE__);
        }elseif ($res === []) {
            abnormalOutputJson(['info'=>'请先登录']);//传了空session_id或查不到
        }

        //查我上传的图片id
        $CardId = D('CardId');
        $udata = $CardId->getCidInfo(array('user_id'=>$user_id),'img_id');
        if(!empty($udata['img_id'])){
            $idArray = explode(',',$udata['img_id']);
            foreach ($idArray as $k => $v) {
                $num ++;//照片总数
            }
        }
        $result = [];
        $Picture = D('Picture');
        $gtE = 0;
        foreach ($idArray as $k => $v) {
            if($k < 9){
                $udata = $Picture->ImgInfo($v);
                if($udata === false){
                    errorOutputJson('j'.__LINE__);
                }elseif ($udata === []) {
                    $rut[$k] = [];
                }else{
                    $rut[$k] = $udata;
                    $rut[$k]['small_path'] = $udata['path'].'.s.jpg';
                }
                $result[0] = $rut;
            }else{
                $udata = $Picture->ImgInfo($v);
                if($udata === false){
                    errorOutputJson('j'.__LINE__);
                }elseif ($udata === []) {
                    $rrut[$gtE] = [];
                }else{
                    $rrut[$gtE] = $udata;
                    $rrut[$gtE]['small_path'] = $udata['path'].'.s.jpg';
                }
                $gtE++;
                $result[1] = $rrut;
            }
        }
        // var_dump($result);
        successOutputJson($result);

    }
    /**
     * 系统照片列表
     */
    public function SystemPhotoWall() {
        $data = inputJson();
        // $data['phpsid'] = '9mtnc4p33ibsnhl0ep533jgoi5';

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录']);
        }

        //查系统默认图片(未下架的)
        $num = D('SystemPics')->where(array('state'=>1))->count();//总数
        $inum = ceil($num/3);
        $result = [];
        for ($i=0; $i < $inum; $i++) { 
            $rows = 3;
            $start = $i*$rows;
            $limit = $start.','.$rows;
            $udata = D('SystemPics')->getAllInfo(array('state'=>1),$limit);

            $Picture = D('Picture');
            foreach ($udata as $key => $value) {
                if(!empty($value['img_id'])){
                    //查图片路径
                    $uudata = $Picture->ImgInfo($value['img_id']);
                    if($uudata === false){
                        errorOutputJson('j'.__LINE__);
                    }elseif ($uudata === []) {
                        $urres[$key] = [];
                    }else{
                        $urres[$key] = $uudata;
                        $urres[$key]['small_path'] = $uudata['path'].'.s.jpg';
                    }
                }else{
                    $urres[$key] = [];
                }
            }
            $result[$i] = $urres;
            unset($urres);
        }
        // var_dump($result);
        successOutputJson($result);

    }

    /**
     * 删除照片
    */
    public function DeletePhoto() {
        $data = inputJson();
        // $data['phpsid'] = 'ofvbs70duforigpk5jcookors0';
        // $data['id'] = 164;//图片id

        //是否登录
        $res = $this->is_login($data['phpsid']);
        if(!$res){
            abnormalOutputJson(['info'=>'请先登录','type'=>1]);
        }

        //查用户id
        $user_id = $this->get_userId($data['phpsid']);
        //查图片id
        $CardId = D('CardId');
        $res = $CardId->getCidInfo(array('user_id'=>$user_id),'img_id');
        if(!empty($res['img_id'])){
            //查图片路径
            $idArray = explode(',',$res['img_id']);
            foreach($idArray as $k=>$v) {
                if($data['id'] == $v){
                    unset($idArray[$k]);//删除图片id
                } 
            }
            $idArray = implode(',',$idArray);
            $idata['img_id'] = $idArray;
            //把新的图片id存入数据表
            $res = $CardId->saveCart(array('user_id'=>$user_id),$idata);
            if($res === false){
                errorOutputJson('j'.__LINE__);
            }else{
                successOutputJson();
            }

        }else{
            abnormalOutputJson(['info'=>'没有可删除的图片','type'=>2]);//拥有0张图片 不可删除
        }
    }

}
